package cn.com.blueInfo.bpm.system.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.system.entity.DynamicFormField;
import cn.com.blueInfo.bpm.system.enums.DynamicFormFieldHidden;
import cn.com.blueInfo.bpm.system.enums.DynamicFormFieldListShow;
import cn.com.blueInfo.bpm.system.enums.DynamicFormFieldRequired;
import cn.com.blueInfo.bpm.system.enums.DynamicFormFieldSearchable;
import cn.com.blueInfo.bpm.system.mapper.DynamicFormFieldMapper;
import cn.com.blueInfo.bpm.system.repository.DynamicFormFieldRepository;
import cn.com.blueInfo.bpm.system.request.DynamicFormFieldDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 动态表单字段表 数据服务实现类
 * @author suxch
 * @since 2025-09-01 19:17:41
 */
@Log4j2
@Service
public class DynamicFormFieldRepositoryImpl
        extends ServiceImpl<DynamicFormFieldMapper, DynamicFormField>
        implements DynamicFormFieldRepository, BaseRepository<DynamicFormField, DynamicFormFieldDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
     * 新增动态表单字段
     * @param dynamicFormField 动态表单字段参数
     */
    @Override
    public Integer createDynamicFormField(DynamicFormField dynamicFormField) {
        return sqlExecuteResult(save(dynamicFormField));
    }

    /**
     * 批量新增动态表单字段
     * @param dynamicFormFieldList 动态表单字段参数列表
     */
    @Override
    public Integer batchCreateDynamicFormField(List<DynamicFormField> dynamicFormFieldList) {
        return sqlExecuteResult(saveBatch(dynamicFormFieldList), dynamicFormFieldList.size());
    }

    /**
     * 删除动态表单字段
     * @param dynamicFormFieldId 动态表单字段ID
     */
    @Override
    public Integer deleteDynamicFormField(Long dynamicFormFieldId) {
        DynamicFormField dynamicFormField = new DynamicFormField();
        dynamicFormField.setId(dynamicFormFieldId);
        dynamicFormField.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(dynamicFormField));
    }

    /**
     * 批量删除动态表单字段
     * @param dynamicFormFieldIdList 动态表单字段ID列表
     */
    @Override
    public Integer batchDeleteDynamicFormField(List<Long> dynamicFormFieldIdList) {
        List<DynamicFormField> dynamicFormFieldList = new ArrayList<>();
        for (Long dynamicFormFieldId : dynamicFormFieldIdList) {
            DynamicFormField dynamicFormField = new DynamicFormField();
            dynamicFormField.setId(dynamicFormFieldId);
            dynamicFormField.setDeleted(BaseDeleted.DELETED);
            dynamicFormFieldList.add(dynamicFormField);
        }
        return sqlExecuteResult(removeBatchByIds(dynamicFormFieldList), dynamicFormFieldList.size());
    }

    /**
     * 更新部分动态表单字段
     * @param dynamicFormField 动态表单字段参数
     */
    @Override
    public Integer updateDynamicFormFieldPartial(DynamicFormField dynamicFormField) {
        validateUpdateId(dynamicFormField.getId());
        LambdaUpdateWrapper<DynamicFormField> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(DynamicFormField::getId, dynamicFormField.getId());
        return sqlExecuteResult(update(dynamicFormField, updateWrapper));
    }

    /**
     * 更新全部动态表单字段
     * @param dynamicFormField 动态表单字段参数
     */
    @Override
    public Integer updateDynamicFormField(DynamicFormField dynamicFormField) {
        validateUpdateId(dynamicFormField.getId());
        return sqlExecuteResult(updateById(dynamicFormField));
    }

    /**
     * 查询一条动态表单字段
     * @param dynamicFormFieldId 动态表单字段ID
     */
    @Override
    public DynamicFormField getDynamicFormFieldById(Long dynamicFormFieldId) {
        LambdaQueryWrapper<DynamicFormField> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DynamicFormField::getId, dynamicFormFieldId);
        return getOne(queryWrapper);
    }

    /**
     * 分页查询动态表单字段
     * @param dynamicFormFieldQueryDTO 动态表单字段参数
     */
    @Override
    public Page<DynamicFormField> getDynamicFormFieldPage(
            DynamicFormFieldDTO.QueryDTO dynamicFormFieldQueryDTO) {
        Page<DynamicFormField> dynamicFormFieldPage = new Page<>(
                dynamicFormFieldQueryDTO.getPage(), dynamicFormFieldQueryDTO.getSize());
        LambdaQueryWrapper<DynamicFormField> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dynamicFormFieldQueryDTO);

        BaseDeleted deleted = dynamicFormFieldQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(dynamicFormFieldPage, queryWrapper);
        } else {
            return page(dynamicFormFieldPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 动态表单字段 数量
     * @param dynamicFormFieldQueryDTO 动态表单字段参数
     */
    @Override
    public Long getDynamicFormFieldCount(DynamicFormFieldDTO.QueryDTO dynamicFormFieldQueryDTO) {
        LambdaQueryWrapper<DynamicFormField> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dynamicFormFieldQueryDTO);

        BaseDeleted deleted = dynamicFormFieldQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 动态表单字段
     * @param dynamicFormFieldQueryDTO 动态表单字段参数
     */
    @Override
    public List<DynamicFormField> getDynamicFormFieldList(
            DynamicFormFieldDTO.QueryDTO dynamicFormFieldQueryDTO) {
        LambdaQueryWrapper<DynamicFormField> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, dynamicFormFieldQueryDTO);

        BaseDeleted deleted = dynamicFormFieldQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<DynamicFormField> queryWrapper,
                              DynamicFormFieldDTO.QueryDTO dynamicFormFieldQueryDTO) {

        Long formId = dynamicFormFieldQueryDTO.getFormId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(formId), DynamicFormField::getFormId, formId);

        String fieldName = dynamicFormFieldQueryDTO.getFieldName();
        queryWrapper.eq(StringUtils.isNotBlank(fieldName), DynamicFormField::getFieldName, fieldName);

        String fieldLabel = dynamicFormFieldQueryDTO.getFieldLabel();
        queryWrapper.eq(StringUtils.isNotBlank(fieldLabel), DynamicFormField::getFieldLabel, fieldLabel);

        String fieldType = dynamicFormFieldQueryDTO.getFieldType();
        queryWrapper.eq(StringUtils.isNotBlank(fieldType), DynamicFormField::getFieldType, fieldType);

        Long fieldDictId = dynamicFormFieldQueryDTO.getFieldDictId();
        queryWrapper.eq(ObjectUtils.isNotEmpty(fieldDictId), DynamicFormField::getFieldDictId, fieldDictId);

        DynamicFormFieldRequired required = dynamicFormFieldQueryDTO.getRequired();
        queryWrapper.eq(ObjectUtils.isNotEmpty(required), DynamicFormField::getRequired, required);

        String defaultValue = dynamicFormFieldQueryDTO.getDefaultValue();
        queryWrapper.eq(StringUtils.isNotBlank(defaultValue), DynamicFormField::getDefaultValue, defaultValue);

        Integer maxLength = dynamicFormFieldQueryDTO.getMaxLength();
        queryWrapper.eq(ObjectUtils.isNotEmpty(maxLength), DynamicFormField::getMaxLength, maxLength);

        String viewRoleIds = dynamicFormFieldQueryDTO.getViewRoleIds();
        queryWrapper.eq(StringUtils.isNotBlank(viewRoleIds), DynamicFormField::getViewRoleIds, viewRoleIds);

        String editRoleIds = dynamicFormFieldQueryDTO.getEditRoleIds();
        queryWrapper.eq(StringUtils.isNotBlank(editRoleIds), DynamicFormField::getEditRoleIds, editRoleIds);

        DynamicFormFieldHidden hidden = dynamicFormFieldQueryDTO.getHidden();
        queryWrapper.eq(ObjectUtils.isNotEmpty(hidden), DynamicFormField::getHidden, hidden);

        String componentType = dynamicFormFieldQueryDTO.getComponentType();
        queryWrapper.eq(StringUtils.isNotBlank(componentType), DynamicFormField::getComponentType, componentType);

        String componentProps = dynamicFormFieldQueryDTO.getComponentProps();
        queryWrapper.eq(StringUtils.isNotBlank(componentProps), DynamicFormField::getComponentProps, componentProps);

        String frameProps = dynamicFormFieldQueryDTO.getFrameProps();
        queryWrapper.eq(StringUtils.isNotBlank(frameProps), DynamicFormField::getFrameProps, frameProps);

        String validationRules = dynamicFormFieldQueryDTO.getValidationRules();
        queryWrapper.eq(StringUtils.isNotBlank(validationRules), DynamicFormField::getValidationRules, validationRules);

        String dependentFields = dynamicFormFieldQueryDTO.getDependentFields();
        queryWrapper.eq(StringUtils.isNotBlank(dependentFields), DynamicFormField::getDependentFields, dependentFields);

        Integer width = dynamicFormFieldQueryDTO.getWidth();
        queryWrapper.eq(ObjectUtils.isNotEmpty(width), DynamicFormField::getWidth, width);

        String align = dynamicFormFieldQueryDTO.getAlign();
        queryWrapper.eq(StringUtils.isNotBlank(align), DynamicFormField::getAlign, align);

        String helpText = dynamicFormFieldQueryDTO.getHelpText();
        queryWrapper.eq(StringUtils.isNotBlank(helpText), DynamicFormField::getHelpText, helpText);

        DynamicFormFieldSearchable searchable = dynamicFormFieldQueryDTO.getSearchable();
        queryWrapper.eq(ObjectUtils.isNotEmpty(searchable), DynamicFormField::getSearchable, searchable);

        DynamicFormFieldListShow listShow = dynamicFormFieldQueryDTO.getListShow();
        queryWrapper.eq(ObjectUtils.isNotEmpty(listShow), DynamicFormField::getListShow, listShow);

        String slotName = dynamicFormFieldQueryDTO.getSlotName();
        queryWrapper.eq(StringUtils.isNotBlank(slotName), DynamicFormField::getSlotName, slotName);

        addQueryWrapper(queryWrapper, dynamicFormFieldQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<DynamicFormField, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                DynamicFormField::getOrderNumber,
                DynamicFormField::getTenantId,
                DynamicFormField::getApplicationId,
                DynamicFormField::getStatus,
                DynamicFormField::getDeleted,
                DynamicFormField::getDeleteUserId,
                DynamicFormField::getDeleteTime,
                DynamicFormField::getRemark,
                DynamicFormField::getCreateUserId,
                DynamicFormField::getCreateTime,
                DynamicFormField::getUpdateUserId,
                DynamicFormField::getUpdateTime,
                DynamicFormField::getExtend1,
                DynamicFormField::getExtend2,
                DynamicFormField::getExtend3);
    }

}
